package com.yiwenup.sorting._01_bubble;

import com.yiwenup.sorting.base.Sort;

/**
 * 冒泡排序
 * 如果在某次排序后就有序了，则提前结束
 **/
public class BubbleSortV2<E extends Comparable<E>> extends Sort<E> {
    @Override
    protected void work() {
        for (int end = array.length - 1; end > 0; end--) {
            boolean flag = true;
            for (int begin = 1; begin <= end; begin++) {
                if (cmp(begin, begin - 1) < 0) {
                    swap(begin, begin - 1);
                    flag = false;
                }
            }
            if (flag) break;
        }
    }
}
